/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - Ex4
 * <p>Powered by webrx On 2022-02-08 10:53:40
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex4 {
    public static void main(String[] args) throws ParseException {
        var s = new ScheduledThreadPoolExecutor(1);
        //在指定的时间内开始 2022-02-08 11:09:51
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long init = sdf.parse("2022-02-08 11:09:05").getTime() - System.currentTimeMillis();
        //s.schedule(() -> {
        //    System.out.printf("%tF %<tT%n", System.currentTimeMillis());
        //    s.shutdownNow();
        //}, init, TimeUnit.MILLISECONDS);

        s.scheduleAtFixedRate(() -> {
            System.out.printf("%tF %<tT%n", System.currentTimeMillis());
        }, init, 5000L, TimeUnit.MILLISECONDS);
    }

    public static void ses(String[] args) {
        //
        ScheduledExecutorService ses = new ScheduledThreadPoolExecutor(1);
        ses.schedule(() -> {
            System.out.println("hello world222");
        }, 1, TimeUnit.SECONDS);

        ses.scheduleAtFixedRate(() -> {
            System.out.println("hello world5");
        }, 5, 1, TimeUnit.SECONDS);
    }

    public static void timer(String[] args) {
        //java.util.Timer
        var t = new Timer();
        //TimerTask
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        }, 3000, 1000);

    }
}
